﻿using System;
using System.IO;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Org.BouncyCastle.Crypto;
using IntelligentEscalator.BUS;
using IntelligentEscalator.DAL;
using IntelligentEscalator.Models;

namespace IntelligentEscalator.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class UserRoleController : ControllerBase
    {
        public IHttpContextAccessor _contextAccessor;
        private JwtSettings _jwtSettings;
        public UserRoleController(IHttpContextAccessor contextAccessor, IOptions<JwtSettings> jwtSettingsAccesser)
        {
            _contextAccessor = contextAccessor;
            _jwtSettings = jwtSettingsAccesser.Value;
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="userinfo">用户信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("AddUser")]
        public ResponseMessage AddUser(UserInfo userinfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                string username = _contextAccessor.HttpContext.Session.GetString("UserName");
                userinfo.creationPerson = username;
                userinfo.creationDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                UserRoleBUS BUS = new UserRoleBUS();
                message = BUS.AddUser(userinfo);
                if (message.errcode == 0)
                {
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "新增用户" + userinfo.username;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "新增用户失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "新增用户";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="userinfo">用户信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateUser")]
        public ResponseMessage UpdateUser(UserInfo userinfo)
        {
            UserRoleBUS BUS = new UserRoleBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.EditUser(userinfo);
                if (message.errcode == 0)
                {
                    string username = _contextAccessor.HttpContext.Session.GetString("UserName");
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "修改用户" + userinfo.username;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "修改用户信息失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "修改用户";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userinfo">用户信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("DelUser")]
        public ResponseMessage DelUser(UserInfo userinfo)
        {
            UserRoleBUS BUS = new UserRoleBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.DelUser(userinfo);
                if (message.errcode == 0)
                {
                    string username = _contextAccessor.HttpContext.Session.GetString("UserName");
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "删除用户" + userinfo.username;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "删除用户失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "删除用户";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 查询用户列表
        /// </summary>
        /// <param name="userinfo">用户信息</param>
        /// <returns>User对象</returns>
        [HttpPost]
        [Route("QueryUserList")]
        public ResponseMessage QueryUserList(UserInfo userinfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                UserRoleBUS BUS = new UserRoleBUS();
                string loginname = _contextAccessor.HttpContext.Session.GetString("UserName");
                message = BUS.QueryUser(userinfo, loginname);
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "查询用户列表异常";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询用户";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 载入用户信息
        /// </summary>
        /// <param name="userinfo">用户信息</param>
        /// <returns>User对象</returns>
        [HttpPost]
        [Route("LoadUser")]
        public ResponseMessage LoadUser(UserInfo userinfo)
        {
            UserRoleBUS BUS = new UserRoleBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.LoadUser(userinfo.id);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "载入用户信息异常";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "载入用户信息";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userinfo">用户信息</param>
        /// <returns>User对象</returns>
        [HttpPost]
        [Route("UpdatePwd")]
        public ResponseMessage UpdatePwd(UserInfo userinfo)
        {
            UserRoleBUS BUS = new UserRoleBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.ResetPwd(userinfo);
                if (message.errcode == 0)
                {
                    string username = _contextAccessor.HttpContext.Session.GetString("UserName");
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "修改密码" + userinfo.username;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "修改密码失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "修改密码";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="roleinfo">角色信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("AddRole")]
        public ResponseMessage AddRole(Role roleinfo)
        {
            UserRoleBUS BUS = new UserRoleBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.AddRole(roleinfo);
                if (message.errcode == 0)
                {
                    string username = _contextAccessor.HttpContext.Session.GetString("UserName");
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "新增角色" + roleinfo.typename;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "新增角色信息失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "新增角色信息";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="roleinfo">角色信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateRole")]
        public ResponseMessage UpdateRole(Role roleinfo)
        {
            UserRoleBUS BUS = new UserRoleBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.EditRole(roleinfo);
                if (message.errcode == 0)
                {
                    string username = _contextAccessor.HttpContext.Session.GetString("UserName");
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "修改角色" + roleinfo.typename;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "修改角色信息失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "修改角色信息";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleinfo">角色信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("DelRole")]
        public ResponseMessage DelRole(Role roleinfo)
        {
            UserRoleBUS BUS = new UserRoleBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.DelRole(roleinfo.id);
                if (message.errcode == 0)
                {
                    string username = _contextAccessor.HttpContext.Session.GetString("UserName");
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "删除角色" + roleinfo.typename;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "删除角色失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "删除角色";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 载入角色信息
        /// </summary>
        /// <param name="roleinfo">角色信息</param>
        /// <returns>Role对象</returns>
        [HttpPost]
        [Route("LoadRole")]
        public ResponseMessage LoadRole(Role roleinfo)
        {
            UserRoleBUS BUS = new UserRoleBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.LoadRole(roleinfo.id);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "载入角色信息失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "载入角色信息";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 查询角色列表信息
        /// </summary>
        /// <param name="roleinfo">角色信息</param>
        /// <returns>User对象</returns>
        [HttpPost]
        [Route("QueryRoleList")]
        public ResponseMessage QueryRoleList(Role roleinfo)
        {
            ResponseMessage message = new ResponseMessage();
            UserRoleBUS BUS = new UserRoleBUS();
            try
            {
                message = BUS.QueryRole(roleinfo);
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "查询角色信息列表失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询角色信息列表";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 获取当前用户信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("GetUserInfo")]
        public ResponseMessage GetUserInfo()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                if (_contextAccessor.HttpContext.Session.GetString("UserId") == "0000")
                {
                    UserInfo userInfo = new UserInfo();
                    userInfo.username = "Fstar1017!";
                    message.errcode = 0;
                    message.data = userInfo;
                }
                else
                {
                    int userid = int.Parse(_contextAccessor.HttpContext.Session.GetString("UserId"));
                    UserRoleBUS BUS = new UserRoleBUS();
                    message = BUS.LoadUser(userid);
                }

            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "获取当前用户信息失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "获取当前用户信息";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 分配用户功能按钮
        /// </summary>
        /// <param name="userinfo">用户信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("AssignPermissions")]
        public ResponseMessage AssignPermissions(UserInfo userinfo)
        {
            UserRoleBUS BUS = new UserRoleBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.AssignPermissions(userinfo);
                if (message.errcode == 0)
                {
                    string username = _contextAccessor.HttpContext.Session.GetString("UserName");
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "分配用户" + userinfo.username + "功能按钮";
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "分配用户功能失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "分配用户功能";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 分配角色巡检功能按钮
        /// </summary>
        /// <param name="role">角色信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateInspectionAuthority")]
        public ResponseMessage UpdateInspectionAuthority(Role role)
        {
            ResponseMessage message = new ResponseMessage();
            UserRoleBUS BUS = new UserRoleBUS();
            try
            {
                message = BUS.UpdateInspectionAuthority(role);
                if (message.errcode == 0)
                {
                    string username = _contextAccessor.HttpContext.Session.GetString("UserName");
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "分配角色" + role.typename + "功能按钮";
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "分配巡检功能";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "分配巡检功能";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }


        /// <summary>
        /// 批量删除用户
        /// </summary>
        /// <param name="userinfo">用户信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("BatchDelUser")]
        public ResponseMessage BatchDelUser(string ids)
        {
            ResponseMessage message = new ResponseMessage();
            UserRoleBUS BUS = new UserRoleBUS();
            try
            {
                message = BUS.BatchDelUser(ids);
                if (message.errcode == 0)
                {
                    string username = _contextAccessor.HttpContext.Session.GetString("UserName");
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "批量删除用户" + ids;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "批量删除用户";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "批量删除用户";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 批量删除角色
        /// </summary>
        /// <param name="ids">角色id</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("BatchDelRole")]
        public ResponseMessage BatchDelRole(string ids)
        {
            UserRoleBUS BUS = new UserRoleBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.BatchDelRole(ids);
                if (message.errcode == 0)
                {
                    string username = _contextAccessor.HttpContext.Session.GetString("UserName");
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "批量删除角色" + ids;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "批量删除角色失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "批量删除角色";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 解锁
        /// </summary>
        /// <param name="ids">角色id</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("Unlock")]
        public ResponseMessage Unlock(int id)
        {
            UserRoleBUS BUS = new UserRoleBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.Unlock(id);
                if (message.errcode == 0)
                {
                    UserRoleDAL da = new UserRoleDAL();
                    UserInfo userInfo = da.LoadUser(id);
                    string username = _contextAccessor.HttpContext.Session.GetString("UserName");
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "解锁用户" + userInfo.username;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "解锁用户失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "解锁用户";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }
    }
}
